package com.anolesoft.amljmes.fortyone.service.mapper;

import cn.hutool.core.util.StrUtil;


import com.anolesoft.amljmes.fortyone.controller.MesGjgyxxFkszInfoController;
import com.anolesoft.amljmes.fortyone.dto.MesGjgyxxFkszInfoDto;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

@Mapper
public interface MesGjgyxxFkszInfoServiceMapper {
    @SelectProvider(type=createSQL.class, method = "queryMesGjgyxxFkszSQL")
    List<MesGjgyxxFkszInfoDto> queryMesGjgyxxFkszSQL(MesGjgyxxFkszInfoController.DemoVO demoVO);
    public static class createSQL {
        public static String queryMesGjgyxxFkszSQL(MesGjgyxxFkszInfoController.DemoVO demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid,a.gyffjc,a.gxmgjzf,a.jzfk,a.cjr,a.cjsj,a.factory_id  from  mes_gjgyxx_fksz a  where 1=1 and a.factory_id = #{factoryId, jdbcType=VARCHAR}");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getGyffjc())){
                        String gyffjc = demoVO.getGyffjc();
                        sql.append(" and upper(a.gyffjc) = upper(#{gyffjc, jdbcType=VARCHAR})");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getGxmgjzf())){
                        String gxmgjzf = demoVO.getGxmgjzf();
                        sql.append(" and upper(a.gxmgjzf) = upper(#{gxmgjzf, jdbcType=VARCHAR})");
                    }
                    if(demoVO.getJzfk()!=null){
                    	sql.append(" and a.jzfk = #{jzfk, jdbcType=INTEGER}");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    @Insert("    insert into mes_gjgyxx_fksz" +
            "      (objuid, gyffjc, gxmgjzf, jzfk, cjr, cjsj, factory_id)" +
            "    values" +
            "      (sys_guid(), trim(#{gyffjc, jdbcType=VARCHAR}), trim(#{gxmgjzf, jdbcType=VARCHAR}), #{jzfk, jdbcType=NUMERIC}, #{cjr, jdbcType=VARCHAR}, #{cjsj, jdbcType=DATE}, #{factoryId, jdbcType=VARCHAR})"
    )
    int addMesGjgyxxFksz(MesGjgyxxFkszInfoDto inFoDtoParam);

    @Update("    update mes_gjgyxx_fksz" +
            "      set gyffjc = trim(#{gyffjc, jdbcType=VARCHAR}),gxmgjzf = trim(#{gxmgjzf, jdbcType=VARCHAR}),jzfk = #{jzfk, jdbcType=VARCHAR}" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateMesGjgyxxFksz(MesGjgyxxFkszInfoDto inFoDtoParam);

    @Delete("delete from mes_gjgyxx_fksz where objuid = #{objuid, jdbcType=VARCHAR} ")
    int deleteMesGjgyxxFksz(MesGjgyxxFkszInfoDto inFoDtoParam);

    @Select(" select count(1) from mes_gjgyxx_fksz where gyffjc = trim(#{gyffjc, jdbcType = VARCHAR}) and factory_id = trim(#{factoryId, jdbcType = VARCHAR}) and objuid != #{objuid, jdbcType = VARCHAR}")
    int isMesGjgyxxFkszName(MesGjgyxxFkszInfoDto inFoDtoParam);
    @Select(" select count(1) from mes_gjgyxx_fksz where gxmgjzf = trim(#{gxmgjzf, jdbcType = VARCHAR}) and factory_id = trim(#{factoryId, jdbcType = VARCHAR}) and objuid != #{objuid, jdbcType = VARCHAR}")
    int isMesGjgyxxFkszName2(MesGjgyxxFkszInfoDto inFoDtoPara);
    
    @Select(" select count(1) from mes_gjgyxx_fksz where gyffjc = trim(#{gyffjc, jdbcType = VARCHAR}) and factory_id = trim(#{factoryId, jdbcType = VARCHAR})")
    int isMesGjgyxxFkszNameXz(MesGjgyxxFkszInfoDto inFoDtoParam);
    @Select(" select count(1) from mes_gjgyxx_fksz where gxmgjzf = trim(#{gxmgjzf, jdbcType = VARCHAR}) and factory_id = trim(#{factoryId, jdbcType = VARCHAR})")
    int isMesGjgyxxFkszNameXz2(MesGjgyxxFkszInfoDto inFoDtoPara);

    @Select("select * from mes_gjgyxx_fksz where factory_id = #{factoryId, jdbcType=VARCHAR} and jzfk = 0")
    List<MesGjgyxxFkszInfoDto> MesGjgyxxFkszAll(String factoryId);
}
